package org.yxj.storagefilemobile;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import org.yxj.lib_storage.StorageFileManager;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        StorageFileManager.newInstance()
                .isDebug(true)
                .setRootFileName("StorageFileMobile")
                .init(this);

        //获取app存储根目录
        StorageFileManager.newInstance().rootPath();
        //获取app存储根目录文件名
        StorageFileManager.newInstance().rootPathName();

        findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.aa);
                ContentValues values = new ContentValues();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
                }
                values.put(MediaStore.Images.Media.DATE_ADDED, new Date().toString());
                values.put(MediaStore.Images.Media.MIME_TYPE,"image/png");
                values.put(MediaStore.Images.Media.MIME_TYPE,  ContentResolver.ANY_CURSOR_ITEM_TYPE);


                ContentResolver contentResolver= getContentResolver();
                Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                try {
                    OutputStream out = contentResolver.openOutputStream(uri);
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
                    out.close();
                    contentResolver.refresh(uri,null,null) ;
                    Toast.makeText(MainActivity.this,"保存成功" ,Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        new LearnContentResolver().init(this);
        new GetPhoneInfoUtil(this);
        PackageManager pm = getPackageManager();

    }
}